package org.apelikecoder.bulgariankeyboard;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

public class StartMeUp extends Activity  {
    
    private final static String screenState = "SCREEN_STATE";
    private final int SCREEN_GO_TO_SETTINGS = 0;
    private final int SCREEN_CHOOSE_INPUT_METHOD = 1;
    private final int SCREEN_CREDITS = 2;
    
    private int mScreen;
    private ViewFlipper masterFlipper;
    private ViewFlipper imageFlipper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        masterFlipper= (ViewFlipper) findViewById(R.id.masterFlipper);
        imageFlipper= (ViewFlipper) findViewById(R.id.imageFlipper);
        imageFlipper.startFlipping();
        if (savedInstanceState == null) {
            mScreen = SCREEN_GO_TO_SETTINGS;
        } else {
            mScreen = savedInstanceState.getInt(screenState);
            masterFlipper.setDisplayedChild(mScreen);
        }
        System.out.println(mScreen);
        //mImageView.setBackgroundResource();
        Button btn = (Button) findViewById(R.id.btnGoToSettings);
        btn.requestFocus();
        btn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                mScreen = SCREEN_CHOOSE_INPUT_METHOD;
                Intent intent = new Intent();
                intent.setAction(android.provider.Settings.ACTION_INPUT_METHOD_SETTINGS);
                try {
                    startActivity(intent);
                } catch (ActivityNotFoundException ex) {
                    Toast.makeText(StartMeUp.this, R.string.input_method_nointent, Toast.LENGTH_LONG).show();
                }
            }
        });

        btn = (Button)findViewById(R.id.btnInputMethod);
        btn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                mScreen = SCREEN_CREDITS; //will open this later
                InputMethodManager mgr = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                mgr.showInputMethodPicker();
            }
        });
        TextView versionInfoView = (TextView) findViewById(R.id.versionInfo);
        try {
            String version = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
            versionInfoView.setText(getString(R.string.version, version));
        } catch (PackageManager.NameNotFoundException ex) {
            versionInfoView.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onRestart() {
        System.out.println("restart " + mScreen);
        imageFlipper.startFlipping();
        if (mScreen == SCREEN_CHOOSE_INPUT_METHOD)
            masterFlipper.setDisplayedChild(SCREEN_CHOOSE_INPUT_METHOD);
        super.onRestart();
    }
    
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus && mScreen == SCREEN_CREDITS)
            masterFlipper.setDisplayedChild(SCREEN_CREDITS);
    }
    
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(screenState, mScreen);
    }
    
    @Override
    protected void onPause() {
        imageFlipper.stopFlipping();
        super.onPause();
    }
    
}
